'''
Created on Apr 17, 2012

This class provides a new abstraction layer for Abstract Classes representation

@author: mekoda
'''
from copy import deepcopy
from main.errors import IOResourceNotFoundedError
from main.process import PCB, Instruction, ProcessStatus, IOInstruction, Process
from main.processHandling import updatePCB


class AbstractClass(object):
    def __init__(self):
        if type(self) is AbstractClass:
            raise Exception('You can t create an instance of an Abstract Class!')
        else:
            pass

#-----------------#

def detectIOResource(anInstruction,iterable):
    foundedYet = False
    for elem in iterable:
        if(not foundedYet):
            if(anInstruction.neededDevice == elem.name):
                foundedYet = True
                return elem
    raise IOResourceNotFoundedError("Could not find IO Resource. Check AAR-AB")
    
#------------------#
    
    
def getRemainingInstructionsOf(aProcess):
    remainingInstructions = myListCopy(aProcess.instructions)
    #Esto que se viene es un flash para obtener las instrucciones que todavia no fueron utilizadas
    del remainingInstructions[:(aProcess.instructions.index(aProcess.pcb.currentInstruction))]
    return remainingInstructions

def getConsumedInstructionsOf(aProcess):
    consumedInstructions = myListCopy(aProcess.instructions)
    indexOfCurrent = aProcess.instructions.index(aProcess.pcb.currentInstruction)
    #Esto que se viene es un flash para obtener las instrucciones que todavia no fueron utilizadas
    del consumedInstructions[indexOfCurrent:]
    return consumedInstructions
    
    
def myListCopy(aList):
    ret = []
    
    for elem in aList:
        ret.append(elem)
    
    return ret
    
pcb1 =  PCB(3)
instruction1 =  Instruction(ProcessStatus.CPU,1)#proceso1 es de Cpu
instruction2 =  Instruction(ProcessStatus.CPU,2)
instruction3 =  Instruction(ProcessStatus.CPU,3)
instruction4 =  IOInstruction(4,'Printer')
instruction5 =  Instruction(ProcessStatus.CPU,5)

#------------------------------------------#
process1 = Process(pcb1)
process1.addInstruction(instruction1)
process1.addInstruction(instruction2)
process1.addInstruction(instruction3)
process1.addInstruction(instruction4)
process1.addInstruction(instruction5)
updatePCB(process1)
updatePCB(process1)

print(process1.pcb.currentInstruction)
print(process1.instructions)
print(getRemainingInstructionsOf(process1))
print(getConsumedInstructionsOf(process1))
